layui.use(['table', 'layer', 'form'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var form = layui.form;
	var username;
	$("#searchBtn").on("click", function() {
		username = $("#username").val();
		if(username == "") {
			layer.msg("请输入搜索条件", {
				time: 500
			});
		} else {
			employeeList();
		}
	});
	$("#reLoad").on("click", function() {
		username = "";
		layer.msg("刷新", {
			time: 500
		}, function() {
			employeeList();
		});
	})
	employeeList();

	function employeeList() {
		$.ajax({
			type: "post",
			url: "/getemployee",
			async: true,
			data: {
				username: username
			},
			success: function(data) {
				var result = $.parseJSON(data);
				var tablelist = table.render({
					elem: '#test',
					height: 'full-95',
					page: true, //开启分页,
					limits: [10],
					limit: 10,
					id: 'table_info',
					cols: [
						[ //表头
							{
								checkbox: true,
								fixed: true
							},
							{
								field: 'username',
								title: '用户名',
								width: 150,
							}, {
								field: 'realname',
								title: '真实姓名',
								width: 150,
							}, {
								field: 'rolename',
								title: '角色',
								width: 150,
							}, {
								field: 'phone',
								title: '手机号',
								width: 180,
							}, {
								field: 'email',
								title: '邮箱',
								width: 178,
							},
							{
								fixed: 'right',
								title: '操作',
								width: 300,
								align: 'center',
								toolbar: '#barDemo'
							}
						]
					],
					"data": result
				});
			}
		});
	}
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'edit') {
			detail(data.id, "edit");
		} else if(layEvent === 'detail') {
			checkDetails(data.id, "detail");
		}else if(layEvent==='del'){
			deleteMore(data.id);
		}
	});
	$("#addBtn").on("click", function() {
		detail(" ", "add");
	});
    //批量删除
   $("#delBtn").on("click", function() {
		var checkStatus = table.checkStatus('table_info');
		if(checkStatus.data.length == 0) {
			layer.alert("请至少选择一条进行删除");
			return;
		}
		var data = checkStatus.data;
		var id = new Array();
		for(var i = 0; i < data.length; i++) {
			id.push(data[i].id);
		}
		deleteMore(id);
	})
	//编辑鲜花信息
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '编辑员工信息',
			type: 2,
			content: '/static/msg/employee/employee_edit.html?id=' + id + "&type=" + type,
			area: ['360px', '380px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var username = body.find(".username").val();
				var realname = body.find(".realname").val();
				var rolename = body.find("#rolename").val();
				var phone = body.find(".phone").val();
				var email = body.find(".email").val();
				var isgo = true;
				var roleid;
				if(!username || username == "" || username == undefined) {
					layer.msg('用户名不能为空', {
						time: 2000
					});
					isgo = false;
				}
				if(isgo) {
					if(!realname || realname == "" || realname == undefined) {
						layer.msg('真实姓名不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					if(!rolename || rolename == "" || rolename == undefined) {
						layer.msg('角色不能为空', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					var phonetest = /^1[3|4|5|7|8][0-9]{9}$/;
					if(!phonetest.test(phone)) {
						layer.msg('请填写正确的手机号', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					var emailtest = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
					if(!emailtest.test(email)) {
						layer.msg('请填写正确的邮箱', {
							time: 2000
						});
						isgo = false;
					}
				}
				if(isgo) {
					$.ajax({
						type: "post",
						url: "/roleget",
						async: true,
						data: {
							rolename: rolename
						},
						success: function(data) {
							var ret = JSON.parse(data);
							roleid = ret[0].id;
							if(roleid) {
								tijiao();
							}
						}
					});
				}

				function tijiao() {
					if(isgo) {
						$.ajax({
							type: "post",
							url: "/employ/" + type,
							data: {
								id: id,
								username: username,
								realname: realname,
								roleid: roleid,
								phone: phone,
								email: email
							},
							async: true,
							success: function(data) {
								layer.msg(data.massage, {
									time: 1000
								}, function() {
									if(data.code == '1') {
										layer.close(index);
										employeeList();
									}
								});
							}
						});
					}
				}

			}
		});
	}
	//查看鲜花信息
	function checkDetails(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '员工详情',
			type: 2,
			content: '/static/msg/employee/employee_edit.html?id=' + id + "&type=" + type,
			area: ['360px', '380px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
	//表格中的删除事件
	function deleteMore(id) {
		if("" == id) {
			layer.alert("请至少选择一条信息!");
			return;
		}
		layer.confirm('确认删除此信息吗？', {
			icon: 3,
			title: '提示'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/employ/delect",
				async: true,
				data: {
					id: id
				},
				success: function(data) {
					layer.msg(data.message);
					employeeList();
				}
			});
			layer.close(index);
		});
	}
});